#! /bin/bash
# Version:0.1
# Description: use to read PSU Refister Value
# Update by kun
# Date:2020/06/17

IPMI=ipmitool

function lnear11_format()
{	
    local rs1=`echo $1 | tr [a-f] [A-F]`
    local rs2=`echo $2 | tr [a-f] [A-F]`
    local x80=`echo "ibase=16; 80" | bc`
    local x78=`echo "ibase=16; 78" | bc`
    local x07=`echo "ibase=16; 07" | bc`
    local x10=`echo "ibase=16; 10" | bc`
    local value1=`echo "ibase=16; $rs1" | bc`
    local value2=`echo "ibase=16; $rs2" | bc`
    local compare=$(($x80&$value2))
    
    if [ $compare -eq 0 ];then
        local value=$value2;
        local temp=$(($(($value2&$x78))>>3))   #value2 右移3位，得到指数
        local value=$(($value&$x07))
        local value=$(($value<<8))
        local value=$(($value+$value1))
        local value=$(($value<<$temp))
	else
		local value=$value2
		local temp=$(($(($value2&$x78))>>3))
		local temp=$(($x10-$temp))        #取补码 
        local temp=$((2**$temp))                              
		local value=$(($value&$x07))
		local value=$(($value<<8))
		local value=$(($value+$value1))
		local value=`echo "scale=3; $value/$temp" | bc`
	fi
    
    echo $value
    return 0
}

function lnear16_format()
{	
    local rs1=`echo $1 | tr [a-f] [A-F]`
    local rs2=`echo $2 | tr [a-f] [A-F]`
    local value=`echo ${rs2}${rs1}`
    local value=`echo "ibase=16; $value" | bc`
    local value=`echo "scale=3; $value/512" | bc`
    echo $value
    return 0
}

function get_vin()
{     
    local i=1
    while [ $i -le 2 ]
    do
        if [ $i -eq 1 ];then
            local device=0xb0
            local tmp=PSU1
        else    
            local device=0xb2
            local tmp=PSU2
        fi
        
        local a=`$IPMI raw 6 0x52 0x0b $device 2 0x88 | awk '{print $1}'`
        local b=`$IPMI raw 6 0x52 0x0b $device 2 0x88 | awk '{print $2}'`
        vin_value=`lnear11_format $a $b`  
        
        printf "%-20s is %-2s %3s\n"  "$tmp VIN raw data" "$a" "$b"
        printf "%-20s is %6s\n"  "$tmp VIN Value" "$vin_value"
        local i=$(($i+1))
    done
    return 0
}

function get_vout()
{     
    local i=1
    while [ $i -le 2 ]
    do
        if [ $i -eq 1 ];then
            local device=0xb0
            local tmp=PSU1
        else    
            local device=0xb2
            local tmp=PSU2
        fi
        
        local a=`$IPMI raw 6 0x52 0x0b $device 2 0x8B | awk '{print $1}'`
        local b=`$IPMI raw 6 0x52 0x0b $device 2 0x8B | awk '{print $2}'`
        vout_value=`lnear16_format $a $b`
        
        printf "%-20s is %-2s %3s\n"  "$tmp VOUT raw data" "$a" "$b"
        printf "%-20s is %6s\n"  "$tmp VOUT Value" "$vout_value"
        local i=$(($i+1))
    done
    return 0
}

function get_iin()
{     
    local i=1
    while [ $i -le 2 ]
    do
        if [ $i -eq 1 ];then
            local device=0xb0
            local tmp=PSU1
        else    
            local device=0xb2
            local tmp=PSU2
        fi
        
        local a=`$IPMI raw 6 0x52 0x0b $device 2 0x89 | awk '{print $1}'`
        local b=`$IPMI raw 6 0x52 0x0b $device 2 0x89 | awk '{print $2}'`
        iin_value=`lnear11_format $a $b`
        
        printf "%-20s is %-2s %3s\n"  "$tmp IIN raw data" "$a" "$b"
        printf "%-20s is %6s\n"  "$tmp IIN Value" "$iin_value"
        local i=$(($i+1))
    done
    return 0
}

function get_iout()
{     
    local i=1
    while [ $i -le 2 ]
    do
        if [ $i -eq 1 ];then
            local device=0xb0
            local tmp=PSU1
        else    
            local device=0xb2
            local tmp=PSU2
        fi
        
        local a=`$IPMI raw 6 0x52 0x0b $device 2 0x8C | awk '{print $1}'`
        local b=`$IPMI raw 6 0x52 0x0b $device 2 0x8C | awk '{print $2}'`
        iout_value=`lnear11_format $a $b`
        
        printf "%-20s is %-2s %3s\n"  "$tmp IOUT raw data" "$a" "$b"
        printf "%-20s is %6s\n"  "$tmp IOUT Value" "$iout_value"
        local i=$(($i+1))
    done
    return 0
}


function get_pin()
{     
    local i=1
    while [ $i -le 2 ]
    do
        if [ $i -eq 1 ];then
            local device=0xb0
            local tmp=PSU1
        else    
            local device=0xb2
            local tmp=PSU2
        fi
        
        local a=`$IPMI raw 6 0x52 0x0b $device 2 0x97 | awk '{print $1}'`
        local b=`$IPMI raw 6 0x52 0x0b $device 2 0x97 | awk '{print $2}'`
        pin_value=`lnear11_format $a $b`
        
        printf "%-20s is %-2s %3s\n"  "$tmp PIN raw data" "$a" "$b"
        printf "%-20s is %6s\n"  "$tmp PIN Value" "$pin_value"
        local i=$(($i+1))
    done
    return 0
}

function get_pout()
{     
    local i=1
    while [ $i -le 2 ]
    do
        if [ $i -eq 1 ];then
            local device=0xb0
            local tmp=PSU1
        else    
            local device=0xb2
            local tmp=PSU2
        fi
        
        local a=`$IPMI raw 6 0x52 0x0b $device 2 0x96 | awk '{print $1}'`
        local b=`$IPMI raw 6 0x52 0x0b $device 2 0x96 | awk '{print $2}'`
        pout_value=`lnear11_format $a $b`
        
        printf "%-20s is %-2s %3s\n"  "$tmp Pout raw data" "$a" "$b"
        printf "%-20s is %6s\n"  "$tmp Pout Value" "$pout_value"
        local i=$(($i+1))
    done
    return 0
}

function get_fan()
{
    local i=1
    while [ $i -le 2 ]
    do
        if [ $i -eq 1 ];then
            local device=0xb0
            local tmp=PSU1
        else    
            local device=0xb2
            local tmp=PSU2
        fi
        
        local a=`$IPMI raw 6 0x52 0x0b $device 2 0x3B | awk '{print $1}'`
        local b=`$IPMI raw 6 0x52 0x0b $device 2 0x3B | awk '{print $2}'`
        fan_duty_value=`lnear11_format $a $b`
        printf "%-20s is %6s\n"  "$tmp fan duty" "$fan_duty_value"
        
        local c=`$IPMI raw 6 0x52 0x0b $device 2 0x90 | awk '{print $1}'`
        local d=`$IPMI raw 6 0x52 0x0b $device 2 0x90 | awk '{print $2}'`
        fan_value=`lnear11_format $c $d`
        printf "%-20s is %-2s %3s\n"  "$tmp fan raw data" "$c" "$d"
        printf "%-20s is %6s\n"  "$tmp fan value" "$fan_value"
        
        local i=$(($i+1))
    done
    return 0
}


#main
echo "**********"
get_vin
echo "**********"
get_vout
echo "**********"
get_iin
echo "**********"
get_iout
echo "**********"
get_pin
echo "**********"
get_pout
echo "**********"
get_fan
